package org.bulu.project.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.bulu.project.system.mapper.FollowMapper;
import org.bulu.project.system.model.entity.Follow;
import org.bulu.project.system.model.query.UserFollowQuery;
import org.bulu.project.system.security.SecurityUtils;
import org.bulu.project.system.service.FollowService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Follow)表服务实现类
 *
 * @author makejava
 * @since 2024-08-19 16:09:12
 */
@Service("followService")
public class FollowServiceImpl implements FollowService {
    @Resource
    private FollowMapper followMapper;


    /**
     * 分页查询所有关注数据
     * @param queryParams
     * @return
     */
    @Override
    public PageInfo checkUserFollow(UserFollowQuery queryParams) {
        queryParams.setUserId(SecurityUtils.getUserId().intValue());
        PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        PageInfo pageInfo = new PageInfo<>(followMapper.checkUserFollow(queryParams));
        return pageInfo;
    }

    /**
     * 获取当前用户所有的关注列表
     */
    @Override
    public List<Follow> getFollowList() {
        try{
            return followMapper.getFollowList(SecurityUtils.getUserId().intValue());
        }catch (Exception e){
            throw new RuntimeException("获取全部关注列表失败");
        }
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @Override
    public int delByIdInt(Integer id) {
        return followMapper.delByIdInt(id);
    }

    /**
     * 添加关注
     * @param follow
     * @return
     */
    @Override
    public int addFollow(@RequestBody Follow follow) {
        follow.setCreateTime(new Date());
        return followMapper.addFollow(follow);
    }

    /**
     * 更新关注
     * @param follow
     * @return
     */
    @Override
    public int updateGroup(Follow follow) {
        return followMapper.updateGroup(follow);
    }

    /**
     * 获取粉丝列表
     * @param queryParams
     * @return
     */
    @Override
    public PageInfo getFansList(UserFollowQuery queryParams) {
        queryParams.setFollowId(SecurityUtils.getUserId().intValue());
        PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        PageInfo pageInfo = new PageInfo<>(followMapper.getFansList(queryParams));
        return pageInfo;
    }
}
